<?php
/**
 * lvsvip
 * 虚拟IP池
 * @author Administrator
 * @version
 */
class Model_Tables_Lvsvip extends Zend_Db_Table_Abstract {
    /**
     * The default table name
     */
    protected $_name = 'lvsvip';
    /**
     * add
     * 增加虚拟IP池
     * @param $params 字段信息
     */
    public function add( $params )
    {
        $data = array(
		'datacenterid' => $params['datacenterid'],
		'virtualipaddress' => $params['virtualipaddress'],
		'port' => $params['port'],
		'app' => $params['app'],
		'delayloop' => $params['delayloop'],
		'lbalgo' => $params['lbalgo'],
		'lbkind' => $params['lbkind'],
		'persistencetimeout' => $params['persistencetimeout'],
		'protocol' => $params['protocol'],
        'usetype' => $params['usetype'],
        );
        return $this->insert($data);
    }
    /**
     * modify
     * 编辑虚拟IP池
     * @param $params 字段信息
     * @param $id 主键值
     */
    public function modify( $params, $id )
    {
        $data = array(
		'datacenterid' => $params['datacenterid'],
		'virtualipaddress' => $params['virtualipaddress'],
		'port' => $params['port'],
		'app' => $params['app'],
		'delayloop' => $params['delayloop'],
		'lbalgo' => $params['lbalgo'],
		'lbkind' => $params['lbkind'],
		'persistencetimeout' => $params['persistencetimeout'],
		'protocol' => $params['protocol'],
        'usetype' => $params['usetype'],
        );
        $where = 'id = '.$id ;
        return $this->update($data , $where );
    }
    /**
     * modifyAjax
     * 无刷新编辑虚拟IP池
     * @param $id 主键值
     * @param $field 字段
     * @param $value 值
     */
    public function modifyAjax( $id, $field, $value )
    {
    	$db = $this->getAdapter();
    	$data = array($field => $value);
    	$where = $db->quoteInto("id=?", $id);
        return $this->update($data , $where );
    }
    /**
     * modifyAjaxRevers
     * 无刷新字段反转虚拟IP池
     * @param $field 需要反转的字段
     * @param $id 主键值
     */
    public function modifyAjaxRevers( $field ,$pri)
    {
    	$db = $this->getAdapter();
    	$sql = "update lvsvip set  $field =not  $field  where id=' $pri'";
		return $db->query($sql);
    }
    /**
     * del
     * 根据条件删除虚拟IP池
     * @param $id 主键值
     */
    public function del( $id )
    {
     	$where = 'id = '.$id;
        return $this->delete($where);
    }
    /**
     * getInfo
     * 获取虚拟IP池信息
     * @param $id 主键值
     * @return 结果数组
     */
    public function getInfo($id)
    {
        $row = $this->fetchRow('id = ' . $id);
        if (!$row) {
        	return null;
        }
        else
        {
        	return $row->toArray();
        }
    }
    /**
     * getCount
     * 获取虚拟IP池数量
     * @param int  搜索条件
     * @return 结果数
     */
    public function getCount($s = "")
    { 
        $select = $this->select()->from($this->_name,'count(*) as num');
    	if($s!="")  
    	{
    		$db = $this->getAdapter();
			$where = $db->quoteInto("id=?", $s);
    		$select = $select->where($where);
    	}
        $result = $this->fetchRow($select)->num;
        return $result;
    }
    /**
     * getList
     * 取虚拟IP池数据分页
     * @param int  页码
     * @param int  每页的行数 
     * @param  搜索条件
     * @return 行数组
     */
    public function getList($page, $numPerPage = 10,$s= "")
    {
    	$page = (1>$page) ? 1 : $page;
    	$offset = ($page-1)*$numPerPage;
        $orderby = array('id');
        if($s!="")     
		{
			$db = $this->getAdapter();
			$where = $db->quoteInto("id= ?", $s);
		}  
        $result = $this->fetchAll($where,$orderby, $numPerPage, $offset )->toArray();
        return $result;
    }
    //根据用途获取vip的值，用途为空则返回全部
	public function getVips($usetype="")//id serverlip port
    {
        $select = $this->select()->from($this->_name, array('id','virtualipaddress','port'))
        ->order("id");
        if($usetype!="")
        	$select = $select->where("usetype='$usetype'");
        $result = $this->fetchAll($select);
    	$roles = array();
        if (!empty($result))
	        {
	            foreach ($result as $item)
	            {
	                $roles[$item['id']] = $item['virtualipaddress'].":".$item['port'];
	            }
	        }
        return $roles;
    }
    //判断ip地址是否属于特定作用的组
    public function ipisusetype($ip,$vipid){
    	$db = $this->getAdapter();
    	$num = $db->fetchOne("select count(*) from lvsservers a left join lvsvip b on a.vip =b.id where b.id=:vipid and serverlip=:ip",array("ip"=>$ip,"vipid"=>$vipid));
    	return $num>0;
    }
    //获取组中编号最小的server地址
    public function firstSr($vipid){
    	$db = $this->getAdapter();
    	return $db->fetchRow("select serverlip,port from lvsservers  where vip=:vipid  order by id  limit 1",array("vipid"=>$vipid));
    }
    //判断ip地址是否属于代理
    //$usetype 代理类型PROXY,PROXYVOD,PROXYTS
    public function ipisproxy($ip,$usetype="PROXY"){
    	
    	$db = $this->getAdapter();
    	$num = $db->fetchOne("select count(*) from lvsservers a left join lvsvip b on a.vip =b.id where usetype=:usetype and serverlip=:ip",array("ip"=>$ip,"usetype"=>$usetype));
    	return $num>0;
    }
    //获取代理组信息
    //$usetype 代理类型PROXY,PROXYVOD,PROXYTS
    public function getproxy($usetype="PROXY")//默认取直播
    {
    	$db = $this->getAdapter();
    		$result=$db->fetchRow("select id,virtualipaddress,port from lvsvip where usetype=:usetype",array("usetype"=>$usetype));
    	return $result;
    }
    //判断组之间是否有包含关系
    public function isSon($father,$son){
    	$db = $this->getAdapter();
    	$sql = "select count(*) from lvsservers where vip=:son and serverip not in (select serverip from lvsservers where vip=:father)";
		$num=$db->fetchOne($sql,array("son"=>$son,"father"=>$father));
		return $num =="0";    	
    }
}
